"""
URLConf for Cab.

Recommended usage is to use a call to ``include()`` in your project's
root URLConf to include this URLConf for any URL beginning with
'/snippets/'.

"""

from django.conf.urls.defaults import *
from django.conf import settings
from django.views.generic.list_detail import object_list
from django.contrib.auth.models import User
from django.contrib.syndication.views import feed
from django.db.models import Sum, Count

import feeds
from models import Language, Snippet, Rating
from views import bookmarks, popular, snippets

from tagging.views import tagged_object_list


from pygameweb.comments.views import generic_comment_wrapper

# Info for feeds.
#feeds = {
#    'author': feeds.SnippetsByAuthorFeed,
#    'language': feeds.SnippetsByLanguageFeed,
#    'latest': feeds.LatestSnippetsFeed,
#    'tag': feeds.SnippetsByTagFeed,
#    }

# Info for generic views.
base_generic_dict = {
    'paginate_by': settings.SNIPPETS_PER_PAGE,
    }

language_info_dict = dict(base_generic_dict,
                          queryset=Language.objects.all())

snippet_info_dict = dict(base_generic_dict,
                         queryset=Snippet.objects.all(),
                         extra_context={"feed_title": "latest snippets",
                                        "subscribe_by_mail": True})


snippet_by_rating_info_dict = dict(base_generic_dict,                         
                         queryset=Snippet.objects.annotate(count=Sum('rating__score')).filter(count__gt=0).order_by("-count"),
                         #queryset=Snippet.objects.annotate(rating=Sum('rating')).order_by("-rating"),
                         #queryset=Rating.objects.top_rated(),
                         extra_context={"show_count":True, 
                                        "title_extra": "positive rated", 
                                        "action":"rated as useful",
                                        "count_title": "score for this snippet",
                                        "feed_url_extra":"rating/",
                                        "feed_title":"snippets by rating"})

snippet_by_bookmarks_info_dict = dict(base_generic_dict,                         
                         queryset=Snippet.objects.annotate(count=Count('bookmark')).filter(count__gt=0).order_by("-count"),
                         #queryset=Snippet.objects.annotate(rating=Sum('rating')).order_by("-rating"),
                         #queryset=Rating.objects.top_rated(),
                         extra_context={"show_count":True, 
                                        "title_extra": "bookmarked",
                                        "action":"bookmarked",
                                        "count_title": "no. of users who bookmarked this snippet",
                                        "feed_url_extra":"bookmarks/",
                                        "feed_title":"snippets by count of bookmarks"})


user_info_dict = dict(base_generic_dict,
                      queryset=User.objects.all(),
                      template_name='cab/user_list.html')
                      
tag_info_dict = dict(base_generic_dict,
                     queryset_or_model=Snippet, allow_empty=True,
                     template_name="cab/snippet_list_by_tag.html",
                     related_tags=True, related_tag_counts=True)

# General snippets views.
urlpatterns = patterns('',
                       #url(r'^(?P<snippet_id>\d+)/$', snippets.snippet_detail, name='snippet_detail'),
                       url(r'^(?P<object_id>\d+)/$', generic_comment_wrapper,
                               {"model": Snippet,
                                "generic_view": snippets.snippet_detail},
                               name='snippet_detail'),
                       url(r'^(?P<snippet_id>\d+)/download/$', snippets.download, name='download'),
                       url(r'^add/$', snippets.create_or_edit_snippet, name='add_snippet'),
                       url(r'^edit/(?P<snippet_id>\d+)/$', snippets.create_or_edit_snippet, name='edit_snippet'),
                       url(r'^languages/(?P<slug>[\w-]+)$', snippets.snippets_by_language, name='snippets_by_language'),
                       url(r'^rate/(?P<snippet_id>\d+)/$', snippets.rate_snippet, name='rate_snippet'),
                       url(r'^tags/(?P<tag>[^/]+)/$', tagged_object_list,
                           tag_info_dict, name='snippets_by_tag'),
#                       url(r'^users/(?P<username>[^/]+)/$', snippets.snippets_by_author, name='snippets_by_author'),
                       )

#    (r'^latest/$', views.overview, {"order_by":"-last_edited"}), 
#    (r'^discussed/$', views.overview, {"order_by":"-comment_count"}), 
#    (r'^alphabet/$', views.overview, {"order_by":"name"}), 
#    (r'^tags/$', views.overview, {"order_by":"tag"}), 
#    url(r'^tags/(.*?)/$', views.overview, {"order_by":"tag"},
#                                    name="project_tag_page"), 
#    (r'^random/$', views.random),
    
# Views that work with bookmarks.
urlpatterns += patterns('',
#                        url(r'^bookmarks/$', bookmarks.bookmarks, name='bookmarks'),
                        url(r'^bookmarks/add/(?P<snippet_id>\d+)/$', bookmarks.add_bookmark, name='add_bookmark'),
#                        url(r'^bookmarks/author/(?P<author_username>[\w-]+)/$', bookmarks.bookmarks_by_author, name='bookmarks_by_author'),
                        url(r'^bookmarks/delete/(?P<bookmark_id>\d+)/$', bookmarks.delete_bookmark, name='delete_bookmark'),
#                        url(r'^bookmarks/language/(?P<language_slug>[\w-]+)/$', bookmarks.bookmarks_by_language, name='bookmarks_by_language'),
#                        url(r'^bookmarks/tag/(?P<tag_slug>[\w-]+)/$', bookmarks.bookmarks_by_tag, name='bookmarks_by_tag'),
                        )

# Views for popular items.
urlpatterns += patterns('',
                        # Need a better URL for this one. (r'^popular/authors/$', popular.top_authors),
                        url(r'^popular/bookmarks/$', popular.most_bookmarked, name='most_bookmarked'),
                        url(r'^popular/languages/$', popular.top_languages, name='top_languages'),
                        url(r'^popular/top-rated/$', popular.top_rated, name='top_rated'),
#                        url(r'^popular/tags/$', popular.top_tags, name='top_tags'),
                        )

# Generic views.
urlpatterns += patterns('django.views.generic.simple',
                        url(r'^$', object_list, snippet_info_dict, name='snippets'),
                        url(r'^random/$', snippets.random, name='random_snippet'),
                        url(r'^rating/$', object_list, snippet_by_rating_info_dict, name='snippets_by_rating'),
                        url(r'^bookmarks/$', object_list, snippet_by_bookmarks_info_dict, name='snippets_by_bookmarks'),
#                        url(r'^rating/$', snippets.snippets_by_rating, name='snippets_by_rating'),
                        url(r'^(?P<snippet_id>\d+)/delete/$', snippets.delete, name="delete_snippet"),
#                        url(r'^feeds/(?P<url>.*)/$', feed, {'feed_dict':feeds}, name='feeds'),
                        url(r'^languages/$', object_list, language_info_dict, name='languages'),
                        url(r'^tags/$', 'direct_to_template',
                            {'template': 'cab/tag_cloud.html'}, name='snippet_tags'),
#                        url(r'^users/$', object_list, user_info_dict, name='users'),
                        )

